home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 25 / AMIGAplus Sonderheft 25 (2000)(Falke)(DE)(Track 1 of 4)[!].iso / PublicDomain / Anwendungen / FloatingClock / Source / fclock.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  12KB  |  428 lines

  1. /************************************************
  2.  *                                                *
  3.  *             Floating clock version 2.1            *
  4.  *                                                *
  5.  *                       BY                        *
  6.  *                                                *
  7.  *                Paul Court (Quinn)                *
  8.  *                    16/06/98                    *
  9.  *                                                *
  10.  ************************************************/
  11.  
  12. #include <exec/types.h>
  13. #include <exec/io.h>
  14. #include <exec/memory.h>
  15. #include <devices/timer.h>
  16. #include <intuition/intuition.h>
  17. #include <dos/dos.h>
  18.  
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21.  
  22. #include <clib/exec_protos.h>
  23. #include <clib/alib_protos.h>
  24. #include <clib/dos_protos.h>
  25. #include <clib/intuition_protos.h>
  26. #include <clib/graphics_protos.h>
  27.  
  28. #include "digits.h"
  29.  
  30. #define WIDTH 100
  31. #define HEIGHT 36
  32.  
  33. struct IntuitionBase* IntuitionBase;    /* Pointer to intuition base */
  34. struct Window* clock_win;                /* Pointer to my window structure. */
  35. struct BitMap my_bitmap;
  36. struct MsgPort* TimerMP;
  37. struct Screen* wbscr;
  38. struct timerequest* TimerIO;
  39. extern struct Border hfiller_border;
  40. extern struct Border vfiller_border;
  41. extern struct Border dfiller_border;
  42.  
  43. /* Window definition! */
  44. struct NewWindow new_clock_win=
  45. {
  46.     10,                    /* Left Edge    x position of window. */
  47.     420,                /* Top Edge        y position of window. */
  48.     WIDTH,                /* Width. */
  49.     HEIGHT,                /* Height. */
  50.     0,                    /* DetailPen     colour register 0. */
  51.     1,                    /* BlockPen        colour register 1. */
  52.     IDCMP_RAWKEY|
  53.     IDCMP_REFRESHWINDOW|
  54.     IDCMP_MOUSEBUTTONS,    /* IDCMP flags. Just to check when the clock is clicked on*/
  55.     SIMPLE_REFRESH|        /* Flags       Intuition should refresh the window. */
  56.     BORDERLESS,
  57.     NULL,                  /* FirstGadget No Custom Gadgets. */
  58.     NULL,                  /* CheckMark   Use Intuition's default CheckMark (v). */
  59.     NULL,                /* Title       Title of the window. */
  60.     NULL,                  /* Screen      Connected to the Workbench Screen. */
  61.     NULL,                  /* BitMap      No Custom BitMap. */
  62.     0,                     /* MinWidth    We do not need to care about these */
  63.     0,                     /* MinHeight   since we have not supplied the window */
  64.     0,                     /* MaxWidth    with a Sizing Gadget. */
  65.     0,                     /* MaxHeight */
  66.     WBENCHSCREEN           /* Type        Connected to the Workbench Screen. */
  67. };
  68.  
  69. void handleIDCMP(struct Window*);
  70. void ReGrab(void);
  71. void CleanUp(void);
  72. void DrawTime(void);
  73.  
  74. /********************
  75.  * Global Variables *
  76.  ********************/
  77. int a=1,b=1,c=1,d=1, dcheck=11;
  78. int going = TRUE;
  79. int left, top, loop;
  80. ULONG timerSig, winSig, gotSig;
  81. ULONG red=255, green=0, blue=0;
  82.  
  83. /*UBYTE mypen;
  84.     extern struct Border hfiller_border;*/
  85.  
  86. main(int argc, char *argv[])
  87. {
  88.     LONG error;
  89.     ULONG hrs,secs,mins;
  90.  
  91.     /**********************************
  92.      * Set the position of the window *
  93.      **********************************/
  94.     if(argc>2)
  95.     {
  96.         new_clock_win.LeftEdge = atoi(argv[1]);
  97.         new_clock_win.TopEdge = atoi(argv[2]);
  98.         left = new_clock_win.LeftEdge;
  99.         top = new_clock_win.TopEdge;
  100.     }
  101.     /*****************************
  102.      * Or print some usage info. *
  103.      *****************************/
  104.     else
  105.     {
  106.         printf("\nFloating Clock Version 2.1");
  107.         printf("\nBy: Paul Court\n");
  108.         printf("\nUsage: FCLOCK <LeftEdge> <TopEdge> [R] [G] [B] (0-255)\n\n");
  109.         exit(0);
  110.     }
  111.  
  112.     if(argc>5)
  113.     {
  114.         red = atoi(argv[3]);
  115.         green = atoi(argv[4]);
  116.         blue = atoi(argv[5]);
  117.     }
  118.  
  119. /******************************
  120.  * Open the intuition library *
  121.  ******************************/
  122. IntuitionBase = (struct IntuitionBase *)
  123.         OpenLibrary( "intuition.library", 0 );
  124.  
  125. if( IntuitionBase != NULL )
  126. {
  127.     /******************************************
  128.      * Get a pointer to the workbench screen. *
  129.      ******************************************/
  130.     wbscr = LockPubScreen( NULL );
  131.     UnlockPubScreen( NULL, wbscr );
  132.  
  133.     hfiller_border.FrontPen = ObtainBestPen(wbscr->ViewPort.ColorMap, red<<24, green<<24, blue<<24, NULL);
  134.     vfiller_border.FrontPen = hfiller_border.FrontPen;
  135.     dfiller_border.FrontPen = hfiller_border.FrontPen;
  136.  
  137.     InitBitMap( &my_bitmap, wbscr->BitMap.Depth, WIDTH, HEIGHT );
  138.     
  139.     for( loop=0; loop < wbscr->BitMap.Depth; loop++ )
  140.     {
  141.         my_bitmap.Planes[loop] = AllocRaster( WIDTH, HEIGHT );
  142.         if( my_bitmap.Planes[loop] == NULL)
  143.         {
  144.             CleanUp(); /* Could not allocate all the memory required */
  145.         }
  146.         BltClear( my_bitmap.Planes[loop], RASSIZE( WIDTH, HEIGHT ), 0);
  147.     }
  148.     BltBitMap( wbscr->RastPort.BitMap, left, top, &my_bitmap, 0, 0, WIDTH, HEIGHT, 0xc0, 0xFF, NULL);
  149.  
  150.     /***************************************
  151.      * We will now try to open the window. *
  152.      ***************************************/
  153.     clock_win = OpenWindow( &new_clock_win ); 
  154.     if( clock_win != NULL )
  155.     {
  156.         /*****************************
  157.          * Set up the timers message *
  158.          * port and the timerIO      *
  159.          *****************************/
  160.         if( TimerMP = CreatePort(0,0) )
  161.         {
  162.             if( TimerIO = (struct timerequest *)
  163.                       CreateExtIO(TimerMP, sizeof(struct timerequest)) )
  164.             {
  165.                 /******************************
  166.                  * Opening the timer device   *
  167.                  * might be useful since this *
  168.                  * is a clock!!                  *
  169.                  ******************************/
  170.                 if (!(error=OpenDevice(TIMERNAME, UNIT_VBLANK, (struct IORequest *)TimerIO, 0L)))
  171.                 {
  172.                     while(going)
  173.                     {
  174.                         /****************************************
  175.                          * Send request to get the system time. *
  176.                          ****************************************/
  177.                         TimerIO->tr_node.io_Command = TR_GETSYSTIME;
  178.                         DoIO( (struct IORequest *) TimerIO);
  179.         
  180.                         /********************
  181.                          * Get the results. *
  182.                          ********************/
  183.                         secs=TimerIO->tr_time.tv_secs;
  184.  
  185.                         /*****************************
  186.                          * Compute days, hours, etc. *
  187.                          *****************************/
  188.                         if(! secs == 0)
  189.                         {
  190.                             mins=secs/60;
  191.                             hrs=mins/60;
  192.                             secs=secs%60;
  193.                             mins=mins%60;
  194.                             hrs=hrs%24;
  195.                         }
  196.  
  197.                         /******************************
  198.                          * Set the individual digits. *
  199.                          ******************************/
  200.                         a=hrs/10;
  201.                         b=hrs%10;
  202.                         c=mins/10;
  203.                         d=mins%10;
  204.                         
  205.  
  206.                         /***********************************
  207.                          * A simple check so the window    *
  208.                          * is only Redrawn if the time has *
  209.                          * changed.                           *
  210.                          ***********************************/
  211.                         if(d != dcheck)
  212.                         {
  213.                             BltBitMapRastPort(&my_bitmap, 0, 0, clock_win->RPort, 0, 0, 140, 50, 0xc0);
  214.                             dcheck = d;
  215.                             DrawTime();
  216.                         }
  217.  
  218.                         /**************************
  219.                          * Send a time request to *
  220.                          * wait a few seconds so  *
  221.                          * that we don't hog the  *
  222.                          * CPU... ;)              *
  223.                          **************************/    
  224.                         TimerIO->tr_node.io_Command = TR_ADDREQUEST;
  225.                         TimerIO->tr_time.tv_secs = 5;
  226.                         SendIO( (struct IORequest *)TimerIO);
  227.                         handleIDCMP(clock_win);
  228.                     }
  229.  
  230.                 /**************
  231.                  * Tidy up... *
  232.                  **************/
  233.                     if( !(CheckIO( (struct IORequest *) TimerIO)) )
  234.                         AbortIO( (struct IORequest *) TimerIO );
  235.                 CloseDevice( (struct IORequest *) TimerIO);
  236.                 }
  237.             DeleteExtIO( (struct IORequest *)TimerIO);
  238.             }
  239.         DeletePort(TimerMP);
  240.         }
  241.     CloseWindow(clock_win);
  242.     }
  243.     /*******************************
  244.      * Free the bitmap information *
  245.      *******************************/
  246.     for( loop=0; loop < wbscr->BitMap.Depth; loop++ )
  247.     {
  248.         if( my_bitmap.Planes[loop] )
  249.             FreeRaster( my_bitmap.Planes[loop], WIDTH, HEIGHT );
  250.     }
  251.     ReleasePen(wbscr->ViewPort.ColorMap,hfiller_border.FrontPen);
  252. CloseLibrary( (struct Library *)IntuitionBase );
  253. }
  254. /* THE END */
  255. }
  256.  
  257. void DrawTime()
  258. {
  259.     /*************************************
  260.      * Use the functions from digits.h to *
  261.      * draw our digital time display     *
  262.      *************************************/
  263.     switch (a)
  264.     {
  265.         case 1 : number_one(AX, clock_win); break;
  266.         case 2 : number_two(AX, clock_win); break;
  267.         case 3 : number_three(AX, clock_win); break;
  268.         case 4 : number_four(AX, clock_win); break;
  269.         case 5 : number_five(AX, clock_win); break;
  270.         case 6 : number_six(AX, clock_win); break;
  271.         case 7 : number_seven(AX, clock_win); break;
  272.         case 8 : number_eight(AX, clock_win); break;
  273.         case 9 : number_nine(AX, clock_win); break;
  274.         case 0 : number_zero(AX, clock_win); break;
  275.     }
  276.     switch (b)
  277.     {
  278.         case 1 : number_one(BX, clock_win); break;
  279.         case 2 : number_two(BX, clock_win); break;
  280.         case 3 : number_three(BX, clock_win); break;
  281.         case 4 : number_four(BX, clock_win); break;
  282.         case 5 : number_five(BX, clock_win); break;
  283.         case 6